<!DOCTYPE html>
<html class="has-navbar-fixed-top">
<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
<title>Git Command - HANZO</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/outdated-browser/1.1.5/outdatedbrowser.min.css">


<meta name="description" content="鬼王, HANZO, THRAEX">



<meta name="keywords" content="鬼王, O70, HANZO, THRAEX">



    <meta name="description" content="Git CommandRestore files1234$ get checkout HEAD &amp;lt;filename&amp;gt;#! discard changes in working directory$ get checkout -- &amp;lt;filename&amp;gt; Show all branches1$ git branch -a Create a local-tracking bran">
<meta name="keywords" content="鬼王, O70, HANZO, THRAEX">
<meta property="og:type" content="article">
<meta property="og:title" content="Git Command">
<meta property="og:url" content="http://yoursite.com/documents/others/git-command.html">
<meta property="og:site_name" content="HANZO">
<meta property="og:description" content="Git CommandRestore files1234$ get checkout HEAD &amp;lt;filename&amp;gt;#! discard changes in working directory$ get checkout -- &amp;lt;filename&amp;gt; Show all branches1$ git branch -a Create a local-tracking bran">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-07-26T04:02:09.431Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git Command">
<meta name="twitter:description" content="Git CommandRestore files1234$ get checkout HEAD &amp;lt;filename&amp;gt;#! discard changes in working directory$ get checkout -- &amp;lt;filename&amp;gt; Show all branches1$ git branch -a Create a local-tracking bran">





<link rel="icon" href="/images/favicon.ico">


<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ovo|Source+Code+Pro">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css">


<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/lightgallery/1.6.8/css/lightgallery.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/justifiedGallery/3.6.5/css/justifiedGallery.min.css">


<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-dark.min.css">

<link rel="stylesheet" href="/css/style.css">

<script defer src="//use.fontawesome.com/releases/v5.0.8/js/all.js"></script>



</head>
<body>
    
<nav class="navbar is-transparent is-fixed-top navbar-main" role="navigation" aria-label="main navigation">
    <div class="container">
        <div class="navbar-brand">
            <a class="navbar-item navbar-logo" href="/">
                
                    
                    HANZO
                    
                
            </a>
            <div class="navbar-burger">
                <span></span>
                <span></span>
                <span></span>
            </div>
        </div>
        
        <div class="navbar-menu navbar-start">
            
            <a class="navbar-item " href="/categories/Technology">Technology</a>
            
            <a class="navbar-item " href="/categories/Life">Life</a>
            
            <a class="navbar-item " href="/archives">Archives</a>
            
            <a class="navbar-item " href="/about">About</a>
            
            <a class="navbar-item " href="/main">Main</a>
            
        </div>
        
        <div class="navbar-menu navbar-end">
            
            <a class="navbar-item search" title="Search" href="javascript:;">
                <i class="fas fa-search"></i>
            </a>
            
            
            
            <a class="navbar-item" title="GitHub" href="https://github.com/O70">
                
                <i class="fab fa-github"></i>
                
            </a>
               
            
        </div>
    </div>
</nav>

    <section class="section">
    <div class="container">
    <article class="article content gallery" itemscope="" itemprop="blogPost">
    <h1 class="article-title is-size-3 is-size-4-mobile" itemprop="name">
        
            Git Command
        
    </h1>
    <div class="article-meta columns is-variable is-1 is-multiline is-mobile is-size-7-mobile">
        <span class="column is-narrow">
            <time datetime="2018-01-21T01:00:00.000Z" itemprop="datePublished">Jan 21 2018</time>
        </span>
        
        
        <span class="column is-narrow">
            
            
            2 minutes read (About 348 words)
        </span>
        
    </div>
    <div class="article-entry is-size-6-mobile" itemprop="articleBody">
    
        <h1 id="Git-Command"><a href="#Git-Command" class="headerlink" title="Git Command"></a>Git Command</h1><h4 id="Restore-files"><a href="#Restore-files" class="headerlink" title="Restore files"></a>Restore files</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ get checkout HEAD &lt;filename&gt;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">#! discard changes in working directory</span></span><br><span class="line">$ get checkout -- &lt;filename&gt;</span><br></pre></td></tr></table></figure>
<h4 id="Show-all-branches"><a href="#Show-all-branches" class="headerlink" title="Show all branches"></a>Show all branches</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -a</span><br></pre></td></tr></table></figure>
<h4 id="Create-a-local-tracking-branch"><a href="#Create-a-local-tracking-branch" class="headerlink" title="Create a local-tracking branch"></a>Create a local-tracking branch</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="hljs-comment">#! git branch &lt;branchname&gt; [&lt;start-point&gt;]</span></span><br><span class="line">$ git branch &lt;branchname&gt; &lt;origin/branch_name&gt;</span><br><span class="line">$ git checkout &lt;branchname&gt;</span><br></pre></td></tr></table></figure>
<p>Combined into a single step with:<br><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -b &lt;branchname&gt; &lt;origin/branch_name&gt;</span><br></pre></td></tr></table></figure></p>
<h4 id="Rename-branch"><a href="#Rename-branch" class="headerlink" title="Rename branch"></a>Rename branch</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -m &lt;oldname&gt; &lt;newname&gt;</span><br></pre></td></tr></table></figure>
<p>Rename a remote-tacking branch and the corresponding reflog:<br><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -m &lt;oldbranch&gt; &lt;newbranch&gt;</span><br><span class="line">$ git push origin :&lt;oldbranch&gt;</span><br><span class="line">$ git push origin &lt;newbranch&gt;</span><br></pre></td></tr></table></figure></p>
<h4 id="Add-remote-repository"><a href="#Add-remote-repository" class="headerlink" title="Add remote repository"></a>Add remote repository</h4><p>Push an existing repository from the command line:<br><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git remote add &lt;originname&gt; &lt;url&gt;</span><br><span class="line">$ git push -u &lt;originname&gt; &lt;branchname&gt;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch --<span class="hljs-built_in">set</span>-upstream-to=origin/&lt;branchname&gt; &lt;localbranchname&gt;</span><br></pre></td></tr></table></figure>
<h4 id="Delete-the-remote-tracking-branch"><a href="#Delete-the-remote-tracking-branch" class="headerlink" title="Delete the remote-tracking branch"></a>Delete the remote-tracking branch</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin --delete &lt;branchname&gt;</span><br></pre></td></tr></table></figure>
<h4 id="Delete-tag"><a href="#Delete-tag" class="headerlink" title="Delete tag"></a>Delete tag</h4><p>Delete existing tags with the given names:<br><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag -d &lt;tagname&gt;...</span><br></pre></td></tr></table></figure></p>
<p>Delete remote tag:<br><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin master :refs/tags/&lt;tagname&gt;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">#! After Git v1.7.0</span></span><br><span class="line">$ git push origin --delete tag &lt;tagname&gt;</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">#! Push an empty branch to the remote branch</span></span><br><span class="line">$ git push origin :&lt;branchname&gt;</span><br></pre></td></tr></table></figure></p>
<h4 id="Local-tag-push-to-remote"><a href="#Local-tag-push-to-remote" class="headerlink" title="Local tag push to remote"></a>Local tag push to remote</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin master --tags</span><br></pre></td></tr></table></figure>
<h4 id="Get-remote-tag"><a href="#Get-remote-tag" class="headerlink" title="Get remote tag"></a>Get remote tag</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git fetch origin tag &lt;tagname&gt;</span><br></pre></td></tr></table></figure>
<h4 id="Version-back"><a href="#Version-back" class="headerlink" title="Version back"></a>Version back</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --hard &lt;commit&gt;</span><br><span class="line">$ git push -f</span><br></pre></td></tr></table></figure>
<h4 id="Squashed-commits"><a href="#Squashed-commits" class="headerlink" title="Squashed commits"></a>Squashed commits</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout master</span><br><span class="line"></span><br><span class="line"><span class="hljs-comment">#! 执行一下命令后，dev上的所有提交已经合并到当前工作区并暂存，但还没有作为一个提交</span></span><br><span class="line">$ git merge --squash dev</span><br><span class="line"></span><br><span class="line">$ git commit –m <span class="hljs-string">'something from dev'</span></span><br></pre></td></tr></table></figure>
<h4 id="Delete-untracked-files"><a href="#Delete-untracked-files" class="headerlink" title="Delete untracked files"></a>Delete untracked files</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git clean -nfd</span><br><span class="line">$ git clean -fd</span><br></pre></td></tr></table></figure>
<ul>
<li><code>-f, --force</code></li>
<li><code>-d</code>: Remove untracked directories in addition to untracked files</li>
</ul>
<h4 id="Modify-commit-message"><a href="#Modify-commit-message" class="headerlink" title="Modify commit message"></a>Modify commit message</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ git stash</span><br><span class="line">$ git rebase HEAD^ --interactive</span><br><span class="line">$ git commit –amend</span><br><span class="line">$ git rebase –<span class="hljs-built_in">continue</span></span><br><span class="line">$ git push -f</span><br></pre></td></tr></table></figure>
<h4 id="Clone-a-specific-branch-or-tag"><a href="#Clone-a-specific-branch-or-tag" class="headerlink" title="Clone a specific branch or tag"></a>Clone a specific branch or tag</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="hljs-built_in">clone</span> --branch=v2.0.2.RELEASE https://github.com/spring-cloud/spring-cloud-netflix.git</span><br></pre></td></tr></table></figure>
<h4 id="Rebase-commit"><a href="#Rebase-commit" class="headerlink" title="Rebase commit"></a>Rebase commit</h4><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout master</span><br><span class="line">$ git rebase dev</span><br><span class="line">$ git push --force</span><br></pre></td></tr></table></figure>
<h3 id="Merge-Commits"><a href="#Merge-Commits" class="headerlink" title="Merge Commits"></a>Merge Commits</h3><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git rebase -i &lt;commit_id&gt;</span><br><span class="line">...pick to squash</span><br><span class="line">$ git push -f</span><br></pre></td></tr></table></figure>
<h3 id="sparse-checkout"><a href="#sparse-checkout" class="headerlink" title="sparse-checkout"></a>sparse-checkout</h3><figure class="highlight sh hljs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir sparses; <span class="hljs-built_in">cd</span> sparses; git init</span><br><span class="line">$ git config core.sparsecheckout <span class="hljs-literal">true</span></span><br><span class="line">$ <span class="hljs-built_in">echo</span> <span class="hljs-string">'FOLDER_NAME'</span> &gt;&gt; .git/info/sparse-checkout</span><br><span class="line">$ git remote add -f origin &lt;repository&gt;</span><br><span class="line">$ git pull --depth=1 origin master</span><br></pre></td></tr></table></figure>

    
    </div>
    
    
    <div class="columns is-mobile is-multiline article-nav">
        <span class="column is-12-mobile is-half-desktop  article-nav-prev">
            
            <a href="/documents/others/rabbitmq-in-action.html">RabbitMQ in Action</a>
            
        </span>
        <span class="column is-12-mobile is-half-desktop  article-nav-next">
            
            <a href="/documents/others/git-flow.html">Git Flow</a>
            
        </span>
    </div>
    
</article>





    </div>
</section>
    <footer class="footer">
    <div class="container">
        <div class="columns content">
            <div class="column is-narrow has-text-centered">
                Copyright &copy; 2019 鬼王&nbsp;
                <!-- Powered by <a href="https://github.com/O70" target="_blank">O70</a> -->
            </div>
            <div class="column is-hidden-mobile"></div>

            
            <div class="column is-narrow">
                <div class="columns is-mobile is-multiline is-centered">
                
                    
                <a class="column is-narrow has-text-black" title="京ICP备19029828号" href="http://www.miitbeian.gov.cn">
                    
                    京ICP备19029828号
                    
                </a>
                
                    
                <a class="column is-narrow has-text-black" title="GitHub" href="https://github.com/O70">
                    
                    GitHub
                    
                </a>
                
                </div>
            </div>
            
            

        </div>
    </div>
</footer>

    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>

<!-- test if the browser is outdated -->
<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/">Update my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/outdated-browser/1.1.5/outdatedbrowser.min.js"></script>
<script>
    $(document).ready(function () {
        // plugin function, place inside DOM ready function
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        })
    });
</script>

<script>
    window.FontAwesomeConfig = {
        searchPseudoElements: true
    }
    moment.locale("en-AU");
</script>



<script src="/js/script.js"></script>


    
    <div class="searchbox ins-search">
    <div class="searchbox-mask"></div>
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="Type something...">
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js"></script>
    
</body>
</html>
